
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Comments;


public class GenericCommentDAO extends AutoKeys implements org.jetnuke.dao.CommentDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.CommentModel#retrieveByPK(int)
	 */
	public Comments retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CommentsModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Comments s = new Comments();
		
		if (rs.next()) {
			fillCommentsFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	
	protected void fillCommentsFromResultSet(Comments s, ResultSet rs) throws Exception{
		s.setCOMMENT(rs.getString("COMMENT"));
		s.setDATE(rs.getTimestamp("DATE1"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setHOST_NAME(rs.getString("HOST_NAME"));
		s.setNAME(rs.getInt("NAME"));
		s.setPID(rs.getInt("PID"));
		s.setREASON(rs.getInt("REASON"));
		s.setSCORE(rs.getInt("SCORE"));
		s.setSID(rs.getInt("SID"));
		s.setSUBJECT(rs.getString("SUBJECT"));
		s.setTID(rs.getInt("TID"));
		s.setURL(rs.getString("URL"));
		
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.CommentModel#countAll()
	 */
	public int countAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CommentsModel.countAll"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.CommentModel#addNew(org.jetnuke.entities.Comments)
	 */
	public int addNew(Comments cm) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("CommentsModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("CommentModel.lastGeneratedCommentID"));
		initNewComment(cm, p);
		
		int id = this.executeAutoKeysQuery(p);
				
		return id;
	}


	/**
	 * @param cm
	 * @param p
	 */
	private void initNewComment(Comments cm, PreparedStatement p) throws Exception{
		p.setString(1,cm.getCOMMENT());
		p.setTimestamp(2,new Timestamp(cm.getDATE().getTime()));
		p.setString(3,cm.getEMAIL());
		p.setString(4,cm.getHOST_NAME());
		p.setInt(5,cm.getNAME());
		p.setInt(6,cm.getPID());
		p.setInt(7,cm.getREASON());
		p.setInt(8,cm.getSCORE());
		p.setInt(9,cm.getSID());
		p.setString(10,cm.getSUBJECT());
		p.setString(11,cm.getURL());
		
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.CommentModel#selectBySID_PID(int, int)
	 */
	public List selectBySID_PID(int id, int pid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CommentsModel.selectBySID_PID"));
		p.setInt(1,id);
		p.setInt(2,pid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		return list;
	}


	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Comments u = new Comments();
			fillCommentsFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.CommentModel#searchOrderDateDesc(java.lang.String)
	 */
	public List searchOrderDateDesc(String query) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CommentsModel.searchOrderDateDesc"));
		p.setString(1,query);
		p.setString(2,query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		return list;
	}


	/* (non-Javadoc)
	 * @see org.jetnuke.model.CommentModel#deleteBySID(int)
	 */
	public void deleteBySID(int sid) throws Exception {
		PreparedStatement p=JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CommentsModel.deleteBySID"));
		p.setInt(1,sid);
		p.executeUpdate();
		p.close();
		
	}

}
